package com.winbomb.simulife.game;

import java.util.List;

import android.graphics.Color;
import android.util.Log;

import com.winbomb.simulife.Master;
import com.winbomb.simulife.World;
import com.winbomb.simulife.base.ICard;
import com.winbomb.simulife.base.IChoice;
import com.winbomb.simulife.base.IOperation;
import com.winbomb.simulife.framework.Game;
import com.winbomb.simulife.framework.Pixmap;
import com.winbomb.simulife.framework.Screen;
import com.winbomb.simulife.framework.Input.TouchEvent;
import com.winbomb.simulife.game.screens.MasterInfoScreen;

public class GameScreen extends Screen {

	// 定义TouchArea
	final TouchArea cardArea = new TouchArea(5, 415, 255, 60);
	final TouchArea optArea = new TouchArea(5, 90, 55, 300);
	final TouchArea pageUpArea = new TouchArea(5, 70, 55, 20);
	final TouchArea pageDownArea = new TouchArea(5, 390, 55, 20);
	final TouchArea nextTurnArea = new TouchArea(255, 415, 60, 60);
	final TouchArea choiceArea = new TouchArea(95, 370, 215, 35);
	final TouchArea masterArea = new TouchArea(5, 5, 50, 50);

	/** 操作的页数，因为操作可能多于5个，因此可能会有多页 */
	private int optPageNum;
	/** 每页几个操作 */
	private static final int OPT_PER_PAGE = 5;

	private float dt;

	/** 对World的引用 */
	private World world;

	private Pixmap mainFrame;
	private String tips;
	private IChoice[] choices;

	List<IOperation> operations;
	ICard[] cards;

	public GameScreen(Game game) {
		super(game);
		this.world = game.getWorld();

		optPageNum = 1;

		operations = world.getOperationList();
		cards = world.getCardList();

		mainFrame = world.getCurrentFrame();
		tips = world.getCurrentTips();
		choices = world.getCurrentChoices();
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {

	}

	@Override
	public void present(float deltaTime) {

		// draw background
		g.drawPixmap(Assets.main_game, 0, 0);

		// draw master's pic
		g.drawRect(5, 5, 50, 50, Color.BLUE);

		Master master = world.getMaster();
		StringBuffer sb = new StringBuffer();
		sb.append(master.getAgeYear());
		sb.append(" 岁 / ");
		sb.append(master.getAgeMonth());
		sb.append(" 月");
		g.drawText(sb.toString(), 10, 80);

		// draw scores
		g.drawText(String.valueOf(master.getMoneyScore()), 75, 36);
		g.drawText(String.valueOf(master.getHappyScore()), 145, 36);
		g.drawText(String.valueOf(master.getAchievScore()), 210, 36);

		// draw master's mode & lucky
		int moodLevel = master.getMoodType().getMoodLevel();
		for (int i = 0; (i < 5) && (i < moodLevel); i++) {
			g.drawPixmap(Assets.star_yellow, 120 + 20 * i, 68);
		}

		int luckLevel = master.getLuckType().getLuckLevel();
		for (int i = 0; (i < 5) && (i < luckLevel); i++) {
			g.drawPixmap(Assets.star_yellow, 253 + 20 * i, 68);
		}

		// draw operations
		if (operations != null && operations.size() > 0) {
			int y = 130;
			for (IOperation opt : operations) {
				g.drawText(opt.getName(), 15, y);
				y += 60;
			}
		}

		// draw cards
		if (cards != null && cards.length > 0) {
			int x0 = 5, y0 = 415;

			for (int i = 0; i < cards.length; i++) {
				int x = x0 + (i % 4) * 65;
				int y = y0 + (i / 4) * 30;

				switch (cards[i].getOpenStatus()) {
				case ICard.CLOSED_STATUS:
					g.drawPixmap(cards[i].getCardIcon(), x, y);
					break;
				case ICard.OPENED_STATUS:
					g.drawPixmap(Assets.card_anim, x, y, 1156, 0, 68, 28);
					break;
				case ICard.OPENNING_STATUS:
					dt += deltaTime;
					int idx = (int) (dt / 0.05);
					g.drawPixmap(Assets.card_anim, x, y, 68 * idx, 0, 68, 28);
					if (idx >= 18) {
						cards[i].setOpenStatus(ICard.OPENED_STATUS);

						this.mainFrame = cards[i].getAnimation();
						this.tips = cards[i].getDescription();
						this.choices = cards[i].getChoices();

						dt = 0;
					}
					break;
				default:
					g.drawPixmap(Assets.card_anim, x, y, 0, 0, 68, 28);
					break;
				}
			}
		}

		// draw content
		if (mainFrame != null) {
			g.drawPixmap(mainFrame, 100, 100);
		} else {
			g.drawPixmap(Assets.no_img, 100, 100);
		}

		if (tips != null) {
			g.drawText(tips, 95, 320);
		} else {
			g.drawText("", 95, 320);
		}

		// draw options
		if (choices == null || choices.length == 0) {
			return;
		}

		float width = 215 / choices.length;
		for (int i = 0; i < choices.length; i++) {
			int x = (int) (95 + width * i);
			g.drawRect(x, 370, (int) width, 35, Color.BLUE);
			g.drawText(choices[i].getChoiceName(), x + 5, 390);
		}
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(float deltaTime) {

		updateTouchEvents();

		for (TouchEvent event : touchEvents) {

			if (event.type != TouchEvent.TOUCH_UP) {
				continue;
			}

			if (world.isOptEnable() && optArea.isBound(event)) {

				// 获取操作在operaitons里的索引号
				int idx = getOperationIndex(event.x, event.y);
				idx += (optPageNum - 1) * OPT_PER_PAGE;

				if (operations == null || idx >= operations.size()) {
					return;
				}

				this.mainFrame = operations.get(idx).getAnimation();
				this.tips = operations.get(idx).getDescription();
				this.choices = operations.get(idx).getChoices();

				Log.d("GameScreen", "Hit :" + idx);
				return;

			} else if (world.isCardEnable() && cardArea.isBound(event)) {
				int idx = getCardIndex(event.x, event.y);

				// 点击已经翻开的牌，无效
				if (ICard.CLOSED_STATUS != cards[idx].getOpenStatus()) {
					return;
				}

				// 设置牌的为翻开状态
				cards[idx].setOpenStatus(ICard.OPENNING_STATUS);
				cards[idx].affect();

				// 增加回合内操作数 并记录这张牌
				world.increaseTurnCount();
				world.getCardManager().record(cards[idx]);

				Log.d("GameScreen", "Hit cards: " + idx);
				return;

			} else if (choiceArea.isBound(event)) {
				if (choices == null || choices.length == 0) {
					return;
				}

				int idx = getOptionIndex(event.x, event.y, choices.length);
				choices[idx].doChoice(game);

				mainFrame = world.getCurrentFrame();
				tips = world.getCurrentTips();
				choices = world.getCurrentChoices();

				return;

			} else if (nextTurnArea.isBound(event) && world.isNextTurnEnable()) {
				world.nextTurn();

				cards = world.getCardList();
				operations = world.getOperationList();

				mainFrame = world.getCurrentFrame();
				tips = world.getCurrentTips();
				choices = world.getCurrentChoices();

				Log.i("GameScreen", "Hit Next Turn");
				return;

			} else if (masterArea.isBound(event)) {
				game.setScreen(new MasterInfoScreen(game));
			}
		}
	}

	/**
	 * 获得点击的位置是属于第几个操作
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	private int getOperationIndex(int x, int y) {

		y = y - 90;

		return y / 60;
	}

	/**
	 * 获得点击的位置是属于第几个卡片
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	private int getCardIndex(int x, int y) {

		y = y - 416;
		return (y / 32) * 4 + x / 64;
	}

	/**
	 * 获得点击的位置属于第几个选择
	 * 
	 * @param x
	 * @param y
	 * @param optCnt
	 *            选项的个数
	 * @return
	 */
	private int getOptionIndex(int x, int y, int optCnt) {

		x = x - 78;

		return x * optCnt / 242;
	}
}
